package com.dreamthen.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @Author zengms
 * @Date 2023-10-26 19:20:45
 **/
public class YouInvocationHandler implements InvocationHandler {

    private Object target;//被代理对象

    public YouInvocationHandler(Object target) {
        this.target = target;
    }

    //被代理对象的增强操作（代理类的执行方法，所有被代理类的方法都会调用这个invoke方法）
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //method：被代理对象中具体方法
        //args：method方法对应的入参
        Object result = null;
        if (null != args) {
            System.out.println("入参：" + Arrays.asList(args));
        } else {
            System.out.println("无入参：");
        }
        try {
            System.out.println("前置通知-before");
            result = method.invoke(target, args);
            System.out.println("后置返回通知-After return");
        }catch (Exception e){
            System.out.println("后置异常通知-After throw");
        }finally {
            System.out.println("后置通知-后置通知-after");
        }
        return result;
    }
}
